var MJProto           = require('./MJProto');
var MJModel           = require('./MJModel');
var GameMessageRouter = 'game.gameHandler.gameMessageNotify';
cc.Class({
    extends: cc.Component,

    properties: {
        MaJiangAtlas: cc.SpriteAtlas,
        hunIcon: cc.Node,
        _onlyid: 0,
        onlyid: {
            get() { return this._onlyid; },
            set(n) { this._onlyid = n; },
        },

        _dir: 0,
        dir: {
            get() { return this._dir; },
            set(n) { this._dir = n; this.setMjFrame(); },
        },

        _show: false,
        isShow: {
            get() { return this._show; },
            set(n) {
                this._show = n;
                if (this._show) {
                    this.isStand = false;
                    if (this._hun) {
                        this.hunIcon.active = true;
                    }
                }
                this.setMjFrame();
            },
        },

        _stand: false,
        isStand: {
            get() { return this._stand; },
            set(n) {
                this._stand = n;
                if (this._stand) {
                    this._show = false;
                }
                this.setMjFrame();
            },
        },

        _mj: 36,
        majiang: {
            get() { return this._mj },
            set(n) { this._mj = n; this.setMjFrame(); },
        },

        _hun: false,
        isHun: {
            get() { return this._hun; },
            set(n) {
                if (this.hunIcon) {
                    this._hun = n;
                    if (this._dir == 0) {
                        this.hunIcon.active = n;
                    } else {
                        if (this.isStand) {
                            this.hunIcon.active = false;
                        } else {
                            this.hunIcon.active = n;
                        }
                    }
                }
            },
        },

        _clickTimes: 0,
        canSend: {
            get() {
                let btn = this.node.getComponent(cc.Button);
                return btn && btn.interactable;
            },
            set(n) {
                let btn = this.node.getComponent(cc.Button);
                btn && (btn.interactable = n);
            },
        },

        _oldy: 0,
        _isSelect: false,

        lastOutFlag: cc.Node,
        flaglast: {
            get() { return this.lastOutFlag && this.lastOutFlag.active; },
            set(n) {
                if(this.lastOutFlag) {
                    this.lastOutFlag.active = n;
                    if(n){
                        this.lastOutFlag.y = 30;
                        let act = [];
                        act[0] = cc.moveBy(0.5, cc.v2(0, 20));
                        act[1] = cc.moveBy(0.5, cc.v2(0, -20));
                        this.lastOutFlag.runAction(cc.repeatForever(cc.sequence(act)));
                    }
                }
            }
        },

		tingFlagNode: cc.Node,
		tingFlag: {
			get () { return this.tingFlagNode.active; },
			set (f) {
				if (this.tingFlagNode) {
					this.tingFlagNode.active = f;
					this.tingFlagNode.stopAllActions();
					if (f) {
						this.tingFlagNode.runAction(cc.repeatForever(cc.sequence([
							cc.moveTo(0.5, cc.v2(0, 74+10)),
							cc.moveTo(0.5, cc.v2(0, 74))
						])));
					}
				}
			},
		},
    },

    // 1 ~ 9 万
    // 11 ~ 19 饼
    // 21 ~ 29 条
    // 31 ~ 34 东南西北 35 中 36 发 37白
    getResName(n) {
		let dirs = ['_D', '_R', '_U', '_L'];

        if (this._show) {
			return ["down2_", "right_", "up_", "left_"][this._dir] + Math.floor(n/10) + "_" + n%10;
        } 
		else {
            if (this._stand) {
				if (this._dir == 0) {
					return 'down_'+Math.floor(n/10)+"_"+(n%10);
				}
				else {
					return 'Stand' + dirs[this._dir];
				}
            } 
			else {
				return 'Sit' + dirs[this._dir];
            }
        }
	},

	getChairID: function () {
		if (MJModel.getChairCount() != 2) {
			return (MJModel.getMyChairID()+this._dir)%4;
		}
		else {
			if (this._dir == 1 || this._dir == 3) { /* 左右两个方向不用 */ 
				return 4;
			}
			else if(this._dir == 0) {
				return MJModel.getMyChairID();
			}
			else if(this._dir == 2) {
				return (MJModel.getMyChairID()+1)%2;
			}
		}
	},

    onLoad() {
        this._oldy = this.node.y;
		this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart.bind(this));
		this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove.bind(this));
		this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd.bind(this));
		this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchCancel.bind(this));
    },

	touchStart: function (event) {
		this.calledFunc = false;
		if (this._dir == 0 && this.isStand) {
			this.beginPos = this.node.getPosition();
		}
	},

	touchMove: function (event) {
		let operateArray = MJModel.getOperateArrayByChairID(this.getChairID());
		if (this._dir == 0 && this.isStand && operateArray && operateArray.indexOf(MJProto.operateType.QI) != -1) {
			this.node.setPosition(
				this.beginPos.x+(event.touch._point.x-event.touch._startPoint.x),
				this.beginPos.y+(event.touch._point.y-event.touch._startPoint.y)
			);
			if (this.node.y >= -290+60) {
				this.showTip(true); 
			}
		}
	},

	touchCancel: function (event) {
		let operateArray = MJModel.getOperateArrayByChairID(this.getChairID());
		if (this._dir == 0 && this.isStand && operateArray && operateArray.indexOf(MJProto.operateType.QI) != -1) {
			if (Math.abs(event.touch._point.y-event.touch._startPoint.y) >= 40) {
				if (this._mj == 35) {
					Global.DialogManager.addPopDialog("确定要打出红中吗？",  () => {
						Global.NetworkManager.notify(GameMessageRouter, MJProto.gameOperateNotify(this._mj, MJProto.operateType.QI));
					}, () => {
						this.node.setPosition(this.beginPos);
					});
				}
				else {
					Global.NetworkManager.notify(GameMessageRouter, MJProto.gameOperateNotify(this._mj, MJProto.operateType.QI));
				}
			}
			this.showTip(false); 
		}
	},

	touchEnd: function (event) {
		let operateArray = MJModel.getOperateArrayByChairID(this.getChairID());
		if (this._dir == 0 && this.isStand && operateArray && operateArray.indexOf(MJProto.operateType.QI) != -1) {
			if (Math.abs(event.touch._point.y-event.touch._startPoint.y) >= 120) {
				if (this._mj == 35) {
					this.calledFunc = true;
					Global.DialogManager.addPopDialog("确定要打出红中吗？",  () => {
						Global.NetworkManager.notify(GameMessageRouter, MJProto.gameOperateNotify(this._mj, MJProto.operateType.QI));
					}, () =>{
						this.node.setPosition(this.beginPos);
					})
				}
				else {
					Global.NetworkManager.notify(GameMessageRouter, MJProto.gameOperateNotify(this._mj, MJProto.operateType.QI));
				}
			}
			else {
				this.node.x = this.beginPos.x;
			}
			this.showTip(false); 
		}
	},

	resetPos: function () {
		if (this.beginPos) {
			this.node.setPosition(this.beginPos);
		}
	},

	showMask: function () {
		let fname = this.getResName(this._mj);
		let maskNode = cc.find('mask', this.node);
		maskNode.active = true;
		maskNode.getComponent(cc.Sprite).spriteFrame = this.MaJiangAtlas.getSpriteFrame(fname);
		// maskNode.scaleX = this.node.scaleX;
		// maskNode.scaleY = this.node.scaleY;
		maskNode.width = this.node.width;
		maskNode.height = this.node.height;
	},

	unshowMask: function () {
		let maskNode = cc.find('mask', this.node);
		maskNode.active = false;
	},

    setMjFrame() {
        let fname = this.getResName(this._mj);

        if (this._dir == 0) {
            this.hunIcon.rotation = 0;
            if (this._show) {
                this.hunIcon.x = -17.4;
                this.hunIcon.y = 33.5;
                this.hunIcon.scaleX = 0.3;
                this.hunIcon.scaleY = 0.3;
            } else {
                this.hunIcon.x = -17;
                this.hunIcon.y = 41;
                this.hunIcon.scaleX = 0.7;
                this.hunIcon.scaleY = 0.7;
            }
        } else if (this._dir == 1) {
            this.hunIcon.rotation = -90;
            this.hunIcon.x = -17;
            this.hunIcon.y = 0;
            this.hunIcon.scaleX = 0.2;
            this.hunIcon.scaleY = 0.2;
        } else if (this._dir == 2) {
            this.hunIcon.rotation = 180;
            this.hunIcon.x = 10.8;
            this.hunIcon.y = -16.4;
            this.hunIcon.scaleX = 0.2;
            this.hunIcon.scaleY = 0.2;
        } else if (this._dir == 3) {
            this.hunIcon.rotation = 90;
            this.hunIcon.x = 17;
            this.hunIcon.y = 14.3;
            this.hunIcon.scaleX = 0.2;
            this.hunIcon.scaleY = 0.2;
        }

        if (this._dir == 0 && this._hun && (this._stand || this._show)) {
            this.hunIcon.active = true;
        } else {
            this.hunIcon.active = this._show && this._hun;
        }
        let spr = this.node.getComponent(cc.Sprite);
        spr.spriteFrame = this.MaJiangAtlas.getSpriteFrame(fname);
		if (this._dir == 0 && (this._stand || this._show)) {
			this.node.setScale(1.1);
		}
    },

    onClick(e, d) {
		if (this.selectFunc && !this.calledFunc) {
			this.selectFunc();
		}
    },

	showTip: function (v) {
		let mjMainNode = this.node.parent.parent;
		let tipNode = cc.find('Tip', mjMainNode);
		if (tipNode) { 
			tipNode.active = v;
		} 
	},
});

